package edu.nf.db.util.convertor;

import edu.nf.db.util.TypeConvertor;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.sql.Date;

/**
 * @author zhu
 * @date 2024-05-09
 */
public class LocalDateConvertor implements TypeConvertor {
    @Override
    public Object convert(Class<?> fieldType, Object value) {
        return switch (value){
            case LocalDate localDate -> localDate;
            case LocalDateTime localDateTime -> localDateTime.toLocalDate();
            case Timestamp timestamp -> timestamp.toLocalDateTime().toLocalDate();
            case Date date -> date.toLocalDate();
            default -> null;
        };
    }

    @Override
    public boolean supports(Class<?> type) {
        return type.equals(LocalDate.class);
    }
}
